/**
 * Created by Y on 2017/3/3.
 * fetch简单封装
 * url: 请求的URL
 * successCallback: 请求成功回调
 * failCallback: 请求失败回调
 * */
export let getRequest = (url,params) => {

    return new Promise((resolve,reject)=> {
        fetch(url, {
            method: 'GET',
            body: JSON.stringify(params)
        })
            .then((response) => response.text())
            .then((responseText) => {
                alert(responseText);
                var res = JSON.parse(responseText);
                if (res.success) {
                    resolve(JSON.parse(responseText));
                } else {
                    reject(res.message);
                }
            })
            .catch((err) => {
                alert(err);
                reject(err);
            });
    })
}


export let postRequest = (url, params) => {
    return new Promise((resolve, reject) => {
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(params)
        })
            .then((response) => {
                console.log('Request Info: ' + response.url);
                return response.json()
            })
            .then((responseData) => {
                console.log('Request Result: '+JSON.stringify(responseData));
                resolve(responseData);
            })
            .catch((err) => {
                console.log('Request Error: '+err)
                reject("网络异常,请稍后再试");
            });
    });
}
